package com.flkin.modules.test.service.impl;

import com.flkin.modules.test.domain.Zoo;
import com.flkin.modules.test.repository.ZooRepository;
import com.flkin.modules.test.service.ZooService;
import com.flkin.modules.test.service.dto.ZooDto;
import com.flkin.modules.test.service.mapstruct.ZooMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import javax.annotation.Resource;

/**
 * @author yanxiang.huang 2020-12-12 13:33:10
 */
@Service
public class ZooServiceImpl implements ZooService {

    @Resource
    private ZooRepository zooRepository;
    @Resource
    private ZooMapper zooMapper;

    @Override
    public List<ZooDto> getAll() {
        return zooMapper.toDto(zooRepository.findAll());
    }

    @Override
    public ZooDto findById(Long id) {
        Zoo zoo = zooRepository.findById(id).orElse(null);
        if (zoo != null) {
            return zooMapper.toDto(zoo);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ZooDto create(Zoo resource) {
        return zooMapper.toDto(zooRepository.save(resource));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(Zoo resource) {
        zooRepository.save(resource);
    }

    @Override
    public void deleteAll(List<Long> ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }

    @Override
    public void deleteById(Long id) {
        zooRepository.deleteById(id);
    }
}